package com.suitsoft.ut.service.commons;

import java.io.File;
import java.util.Date;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suitsoft.ut.dao.commons.UploadFileDao;
import com.suitsoft.ut.entity.commons.UploadFileInfo;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.service.ServiceException;
import com.suitsoft.ut.util.upload.UploadFileUtil;

/**
 *
 *@author Suine
 */
@Service
@Transactional
public class UploadFileManager {
	
	public static final int MIDDLE = 1;
	public static final int DABIAN = 2;
	public static final int ALL = 3;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private UploadFileDao dao;
	
	public UploadFileInfo get(Long id){
		return dao.get(id);
	}
	
	public void save(UploadFileInfo file){
		dao.save(file);
	}
	
	public void delete(Long id){
		dao.delete(id);
	}
	
	public void upload(File uploadFile,Thesis thesis,String uploadFileFileName,int f,ServletContext sc){
		if(uploadFile == null){
			throw new ServiceException();
		}else{
			UploadFileUtil fileUtil = new UploadFileUtil(sc,uploadFileFileName);
			fileUtil.CreateTmpFileName();
			File tempFileName = new File(fileUtil.getNewFileNamePath());
			uploadFile.renameTo(tempFileName);
			
			UploadFileInfo uploadFileInfo = new UploadFileInfo(tempFileName,uploadFileFileName,thesis);
			uploadFileInfo.setMiddleDabian(Long.valueOf(f));
			uploadFileInfo.setPostedTime(new Date());
			save(uploadFileInfo);
		}
	}

}

